package ikelib;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;

public class menuUtil {
	
	/**
	 * メニューバーから指定のメニューを検索する。
	 * @param mb
	 * @param name
	 * @return
	 */
	public static JMenu getMenu(JMenuBar mb , String name){
		int max = mb.getMenuCount();
		for(int i=0;i<max;i++){
			JMenu menu = mb.getMenu(i);
			String str = menu.getText();
			if(str.compareToIgnoreCase(name)==0){
				return menu;
			}
		}
		return null;

	}
	
	public static JMenu addMenu(JPopupMenu popup,String name) {
		JMenu item = new JMenu(name);
		popup.add(item);
		return item;
	}
	
	
	// MENU セットの補助
	public static JMenu setupMenu(String menustr,int mnemonic,String cmd , ActionListener al){
		JMenu ret = new JMenu(menustr);
		if(mnemonic!=0) ret.setMnemonic(mnemonic);
		if(al!=null){
			ret.addActionListener(al);
		}
		ret.setActionCommand(cmd);
		return ret;
	}
	public static JMenuItem setupMenuItem(String menustr,int mnemonic,String cmd , ActionListener al){
		JMenuItem ret = new JMenuItem(menustr);
		if(mnemonic!=0) ret.setMnemonic(mnemonic);
		if(al!=null){
			ret.addActionListener(al);
		}
		ret.setActionCommand(cmd);
		return ret;
	}

	
	/*　JPopupMenu に JMenuItemを追加する　
	 * 		使い方
	 * addPopupMenuItem(PopupMenuAmp,"Offset Scale ON (all channel)", new ActionListener(){ 
			public void actionPerformed(ActionEvent e) {
				doScaleOffsetAll(true);
			}

	    });

	 */
	public static JMenuItem addMenuItem(JPopupMenu popup,String name, ActionListener al) {
		JMenuItem item = new JMenuItem(name);
		item.addActionListener(al);
		popup.add(item);
		return item;
	}
	/*
	 * JPopupMenu に JMenuItemを追加する　（イメージアイコン付き）
	 */
	public static JMenuItem addMenuItem(JPopupMenu popup,String name, ActionListener al,ImageIcon icon) {
		JMenuItem item = addMenuItem(popup,name,al);//@
		item.setIcon(icon);
		return item;
	}
	/*
	 *  JMenu に JMenuItemを追加する　
	 */
	public static JMenuItem addMenuItem(JMenu popup,String name, ActionListener al) {
		JMenuItem item = new JMenuItem(name);
		item.addActionListener(al);
		popup.add(item);
		return item;
	}
	/*
	 *  JPopupMenu に JCheckBoxMenuItemを追加する　
	 */
	public static JCheckBoxMenuItem addCheckBoxMenuItem(JPopupMenu popup,String name, ActionListener al) {
		JCheckBoxMenuItem item = new JCheckBoxMenuItem(name);
		item.addActionListener(al);
		popup.add(item);
		return item;
	}
	/*
	 *  JMenu に JCheckBoxMenuItemを追加する　
	 */
	public static JCheckBoxMenuItem addCheckBoxMenuItem(JMenu popup,String name, ActionListener al) {
		JCheckBoxMenuItem item = new JCheckBoxMenuItem(name);
		item.addActionListener(al);
		popup.add(item);
		return item;
	}
	/*
	 *  JPopupMenu に JRadioButtonMenuItemを追加する　
	 */
	public static JRadioButtonMenuItem addRadioButtonMenuItem(JPopupMenu popup,String name, ActionListener al) {
		JRadioButtonMenuItem item = new JRadioButtonMenuItem(name);
		item.addActionListener(al);
		popup.add(item);
		return item;
	}
	/*
	 *  JMenu に JRadioButtonMenuItemを追加する　
	 */
	public static JRadioButtonMenuItem addRadioButtonMenuItem(JMenu popup,String name, ActionListener al) {
		JRadioButtonMenuItem item = new JRadioButtonMenuItem(name);
		item.addActionListener(al);
		popup.add(item);
		return item;
	}
}
